import jwtDecode from 'jwt-decode'
import router from '../router'
import vueMain from '../main'

const tokenKey = 'access_token'

// 获取token
export function getToken () {
  return localStorage.getItem(tokenKey)
}

// 设置token
export function setToken (value) {
  localStorage.setItem(tokenKey, value)
}

// 清除token
export function removeToken () {
  localStorage.removeItem(tokenKey)

  vueMain.$notify({
    title: '警告',
    message: '登录状态已失效，即将跳转到登录页',
    type: 'warning'
  })
  setTimeout(() => {
    router.push('/login')
  }, 3000)
}

// 验证是否登录
export function isAuth () {
  if (getToken()) { return true } else { return false }
}

// 解析token
export function getTokenValueData () {
  const token = getToken()
  if (token) { return jwtDecode(token) }
  return null
}
